<?php
function _md5($str){
	return md5(SALT . md5(SALT. $str . SALT) .SALT);
}

function _getDate($type = 1,$time = 0){
	$dataTime = '';//当前时间为空
	switch($type){
		case 1://第一种是当前的时间
			$dataTime = date('Y-m-d H:i:s');
			break;
		case 2://第二种是时间戳
			$dataTime = time();
			break;
		case 3://第三种是将时间戳转化为时间日期
			$dataTime = data('Y-m-d H:i:s',time());
			break;
		case 4://第四种是将日期格式转华为时间戳
			$dataTime = strtotime($time);
			break;
	}
	return $dataTime;
}

//单个上传头像
function uploadFile($filedata,$filepath = 'Upload/img/'){
	if(empty($filedata)){
		return ['status' => 0,'mess' => '没有上穿头像！'];
	}	
	$typearr = ['image/jpeg','image/png','image/gif','image/jpg'];
	if(!in_array($filedata['type'],$typearr)){
		return ['status' => 0, 'mess' => '图片格式不符合！'];
	}
	if($filedata['size'] > 204800){
		return ['status' => 0,'mess' => '图片大小不符合规定'];  
	}
	$filename = time().'.'.end(explode('.',$filedata['name']));
	if(!is_dir($filepath) && !is_writable($filepath)){
		return ['status' => 0, 'mess' => '头像文件存储不存在！'];
	}
	$imgpath = $filepath.$filename;
	if(!move_uploaded_file($filedata['tmp_name'],$imgpath)){
		return ['status' => 0, 'mess' => '头像上传不成功！']; 
	}
	return $imgpath;
}

//多头像上传
function uploadFiles($filedata,$filepath = 'Upload/img/'){
	if(empty($filedata)){
		return ['status' => 0,'mess' => '没有上穿头像！'];
	}	
	$typearr = ['image/jpeg','image/png','image/gif','image/jpg'];
	if(!in_array($filedata['type'],$typearr)){
		return ['status' => 0, 'mess' => '图片格式不符合！'];
	}
	if($filedata['size'] > 204800){
		return ['status' => 0,'mess' => '图片大小不符合规定'];  
	}
	$filename = microtime(true).'.'.end(explode('.',$filedata['name']));
	if(!is_dir($filepath) && !is_writable($filepath)){
		return ['status' => 0, 'mess' => '头像文件存储不存在！'];
	}
	$imgpath = $filepath.$filename;
	if(!move_uploaded_file($filedata['tmp_name'],$imgpath)){
		return ['status' => 0, 'mess' => '头像上传不成功！']; 
	}
	return $imgpath;
}

//无限极分类
/**
 * 把返回的数据集转换成Tree
 * @param array $list 要转换的数据集
 * @param string $pk 自增字段（栏目id）
 * @param string $pid parent标记字段
 * @return array
 *root 是节点默认是0
 将数据库读出来的数组进行循环遍历赋给
 */
function makeTree($data,$pk = 'id',$pid = 'pid', $child = '_child',$root = 0){
	$tree = [];
	$packdata = [];
	foreach($data as $v){
		$packdata[$v[$pk]] = $v;//将$v赋给数组下标从1开始的数组
	}
	//在把这个新数组进行遍历，如果pid 节点等于0的话就将值引用给$tree[]数组 
	foreach($packdata as $key => $val){
		if($val[$pid] == $root){
			$tree[] =& $packdata[$key];
		}else{
			$packdata[$val[$pid]][$child][] =& $packdata[$key];//如果不等于0.就将pid = 1 就是数组下标等于1的在追加个下标为child把值赋给它
		}		
	}
	return $tree;
}
//利用递归函数将无限极分类
function _getTree($data, $pid = 0, $level = 0, $html = '&nbsp;'){
	$arr = [];
	foreach ($data as $v) {
		if($v['pid'] == $pid){
			$v['level'] = $level + 1;
			$v['html'] = str_repeat($html, $level);//用一写字符将其区分等级
			$arr[] = $v;//将数据库读出来的数组进行循环遍历赋给新数组
			$arr = array_merge($arr,_getTree($data,$v['id'],$level + 1,$html));
		}
	}
	return $arr;
}

//将数组循环遍历出来后开始一个一个匹配pid等于0的往下进行否则跳出

















